package com.copa.l989;

import java.util.LinkedList;
import java.util.List;

/**
 * @author copa
 * @createDate 2022-07-07 18:42
 * @function 链表逐位相加（优化版）
 */
public class Test2 {

    public List<Integer> addToArrayForm(int[] num, int k) {
        LinkedList<Integer> result = new LinkedList<>();
        int next = 0;
        for (int i = num.length - 1; i >= 0; i--) {
            int remainer = k % 10;
            k = k / 10;
            int total = num[i] + remainer + next;
            next = total / 10;
            result.addFirst(total % 10);
        }
        k = k + next;
        while (k != 0) {
            result.addFirst(k % 10);
            k = k / 10;
        }
        return result;
    }

    public static void main(String[] args) {
        //int[] num = {1,2,0,0};
        //int k = 34;
        int[] num = {7};
        int k = 993;
        System.out.println(new Test2().addToArrayForm(num, k));
    }
}
